/* Copyright 2001,2002,2003 NAH6 BV
 * All Rights Reserved
 *
 *  $Header: /var/lib/cvs/secphone/ui/rng/sources/esrcCryptoApi.cpp,v 1.4 2003/11/21 16:12:58 itsme Exp $
 *
 *
 * this module implements entropy gathering from the microsoft crypto api
 *
 * this is called at regular intervals.
 *
 * only on the win32 desktop version this provides proper random.
 *
 */


#include <windows.h>    // nescesary for wincrypt
#include <wincrypt.h>

#include "debug.h"

#include "esrcCryptoApi.h"

esrcCryptoApi::esrcCryptoApi()
{
    if (!CryptAcquireContext(&m_hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
    {
        error("esrcCryptoApi: ginitializing cryptoapi\n");
        m_hProv= NULL;
    }
}

esrcCryptoApi::~esrcCryptoApi()
{
    if (m_hProv!=NULL)
    {
        CryptReleaseContext(m_hProv, 0);
        m_hProv= NULL;
    }
}
bool esrcCryptoApi::IsPolledSource() const
{
    return true;
}
void esrcCryptoApi::HandleSource()
{
    ByteVector data; data.resize(16);
    if (!CryptGenRandom(m_hProv, data.size(), vectorptr(data)))
    {
        error("esrcCryptoApi: getting random data\n");
        return;
    }
    AddRandom(data);
}

